package com.example.agriapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.agriapp.model.bean.FeedItem;
import com.example.agriapp.R;

import java.util.List;

public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedViewHolder> {
    private List<FeedItem> feedItems;
    private OnFeedInteractionListener listener;
    private Context context;

    public interface OnFeedInteractionListener {
        void onShopClick(FeedItem item);
        void onFollowClick(FeedItem item);
        void onLikeClick(FeedItem item);
        void onCommentClick(FeedItem item);
        void onShareClick(FeedItem item);
    }

    public FeedAdapter(Context context, List<FeedItem> feedItems, OnFeedInteractionListener listener) {
        this.context = context;
        this.feedItems = feedItems;
        this.listener = listener;
    }

    @Override
    public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_feed, parent, false);
        return new FeedViewHolder(view);
    }

    @Override
    public void onBindViewHolder(FeedViewHolder holder, int position) {
        FeedItem item = feedItems.get(position);
        holder.bind(item, listener);
    }

    @Override
    public int getItemCount() {
        return feedItems.size();
    }

    public void updateData(List<FeedItem> newItems) {
        this.feedItems = newItems;
        notifyDataSetChanged();
    }

    public void updateItem(FeedItem updatedItem) {
        for (int i = 0; i < feedItems.size(); i++) {
            if (feedItems.get(i).getId().equals(updatedItem.getId())) {
                feedItems.set(i, updatedItem);
                notifyItemChanged(i);
                break;
            }
        }
    }

    static class FeedViewHolder extends RecyclerView.ViewHolder {
        private final ImageView btnLike;
        private final ImageView btnComment;
        private ImageView ivAvatar;
        private TextView tvUsername;
        private TextView tvDate;
        private TextView btnShop;
        private TextView btnFollow;
        private TextView tvContent;
        private ImageView ivPostImage;
        private TextView tvTag;
        private TextView tvLikeCount;
        private TextView tvCommentCount;

        public FeedViewHolder(View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvDate = itemView.findViewById(R.id.tv_date);
            btnShop = itemView.findViewById(R.id.btn_shop);
            btnFollow = itemView.findViewById(R.id.btn_follow);
            tvContent = itemView.findViewById(R.id.tv_content);
            ivPostImage = itemView.findViewById(R.id.iv_post_image);
            tvTag = itemView.findViewById(R.id.tv_tag);
            tvLikeCount = itemView.findViewById(R.id.tv_like_count);
            tvCommentCount = itemView.findViewById(R.id.tv_comment_count);
            
            // 点赞按钮
            btnLike = itemView.findViewById(R.id.btn_like);
            // 评论按钮
            btnComment = itemView.findViewById(R.id.btn_comment);
//            // 分享按钮
//            TextView btnShare = itemView.findViewById(R.id.btn_share);
        }

        public void bind(final FeedItem item, final OnFeedInteractionListener listener) {
            // 设置头像 (实际项目中应使用Glide/Picasso加载)
            ivAvatar.setImageResource(R.drawable.default_avatar);
            
            // 设置用户名 (按照图片中的格式)
            tvUsername.setText(item.getUsername());
            
            // 设置日期
            tvDate.setText(item.getDate());
            
            // 设置"进店铺"按钮 (根据图片中显示)
            btnShop.setVisibility(item.hasShop() ? View.VISIBLE : View.GONE);
            btnShop.setOnClickListener(v -> listener.onShopClick(item));
            if (item.isFollowed())
                btnFollow.setVisibility(View.GONE);
            else
                btnFollow.setVisibility(View.VISIBLE);
            // 设置关注按钮 (按照图片中的"+关注"样式)
            btnFollow.setText(item.isFollowed() ? "已关注" : "+关注");
            btnFollow.setOnClickListener(v -> listener.onFollowClick(item));
            
            // 设置内容文本
            tvContent.setText(item.getContent());
            
            // 设置帖子图片 (根据图片中的相亲节目海报)
//            if (item.getImageRes() != 0) {
//                ivPostImage.setVisibility(View.VISIBLE);
//                ivPostImage.setImageResource(item.getImageRes());
//            } else {
//                ivPostImage.setVisibility(View.GONE);
//            }
            Log.e("TAG", "onBindViewHolder: "+item.getImageUri() );
            if (item.getImageUri() != null){
                ivPostImage.setVisibility(View.VISIBLE);
                Glide.with(itemView.getContext())
                        .load(item.getImageUri())
                        .into(ivPostImage);
            }else ivPostImage.setVisibility(View.GONE);


            if (item.getLikes()==0){
                btnLike.setImageResource(R.drawable.ic_love_no);
            }else {
                btnLike.setImageResource(R.drawable.ic_love_yes);
            }
            // 设置标签 (如"月老红缘")
            if (item.getTag() != null && !item.getTag().isEmpty()) {
                tvTag.setVisibility(View.VISIBLE);
                tvTag.setText(item.getTag());
                // 设置标签样式 (根据图片中的红色标签)
               // tvTag.setBackgroundResource(R.drawable.bg_tag_red);
                tvTag.setTextColor(Color.WHITE);
            } else {
                tvTag.setVisibility(View.GONE);
            }
            
            // 设置点赞和评论数 (根据图片中的"1"和"0")
            tvLikeCount.setText(String.valueOf(item.getLikes()));
            tvCommentCount.setText(String.valueOf(item.getComments()));
            
            // 设置点赞按钮点击事件
            itemView.findViewById(R.id.btn_like).setOnClickListener(v -> {
                listener.onLikeClick(item);
            });
            
            // 设置评论按钮点击事件
            itemView.findViewById(R.id.btn_comment).setOnClickListener(v -> {
                listener.onCommentClick(item);
            });
            
            // 设置分享按钮点击事件
            itemView.findViewById(R.id.btn_share).setOnClickListener(v -> {
                listener.onShareClick(item);
            });
        }
    }
}